package com.sc.nft.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.entity.SecondCollection;
import com.sc.nft.entity.dto.MaintainUserConSignCollectionPageDTO;
import com.sc.nft.entity.dto.MarketConsignPageDTO;
import com.sc.nft.entity.dto.SecondConSignCollectionDTO;
import com.sc.nft.entity.vo.ConsignmentSalesIdGetParticularsVO;
import com.sc.nft.entity.vo.MarketConsignPageV3_7VO;
import com.sc.nft.entity.vo.MyConsignmentListVO;
import com.sc.nft.entity.vo.MySalestListVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 寄售表(sc_second_collection)数据Mapper
 *
 * @author kancy
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2022-10-05 00:23:53
 */
@Mapper
public interface SecondCollectionMapper extends BaseMapper<SecondCollection> {

    Page<SecondConSignCollectionDTO> getAdminConsignPage(IPage page, @Param("startTime") Date startTime, @Param("endTime") Date endTime, @Param("status") Integer status,
                                                         @Param("phone") String phone, @Param("marketType") Integer marketType);

    Long getAdminConsignPageTol(@Param("startTime") Date startTime, @Param("endTime") Date endTime, @Param("status") Integer status,
                                @Param("phone") String phone, @Param("marketType") Integer marketType);

//    Page<ConSignCollectionPageDTO> getConsignPage(IPage page, @Param("collectionId") Long collectionId, @Param("sortType") Integer sortType);

//    Page<MyConSignCollectionPageDTO> userConsignPage(IPage page, @Param("status") Integer status, @Param("userId") Long userId);


    Page<MaintainUserConSignCollectionPageDTO> maintainUserConsignPage(IPage page, @Param("status") Integer status,
                                                                       @Param("userId") Long userId,
                                                                       @Param("lowPrice") BigDecimal lowPrice,
                                                                       @Param("highPrice") BigDecimal highPrice,
                                                                       @Param("collectionId") Long collectionId);

    List<Long> findMaintainUserLowPriceConsign(@Param("userId") Long userId, @Param("collectionId") Long collectionId
            , @Param("num") Integer num, @Param("lowPrice") BigDecimal lowPrice, @Param("highPrice") BigDecimal highPrice);

    /**
     * 寄售列表(品牌孵化/自由二级)
     *
     * @param page
     * @param collectionId
     * @param sortType
     * @return
     */
    Page<MarketConsignPageDTO> marketConsignPage(IPage page, @Param("collectionId") Long collectionId, @Param("sortType") Integer sortType);

    /**
     * 分页查询寄售中的藏品
     *
     * @param page
     * @param userId
     * @return
     */
    Page<MyConsignmentListVO> myConsignmentList(Page page, @Param("userId") Long userId);

    /**
     * 分页查询我的挂单
     *
     * @param page
     * @param userId
     * @return
     */
    Page<MySalestListVO> mySalestList(Page page, @Param("userId") Long userId);

    /**
     * 后台市值账号操作专用
     *
     * @param userId
     * @param collectionId
     * @return
     */
    List<MySalestListVO> maintainSalestList(@Param("userId") Long userId, @Param("collectionId") Long collectionId,
                                            @Param("lowPrice") BigDecimal lowPrice, @Param("highPrice") BigDecimal highPrice);


    /**
     * 获取当前有多少个上架的商品
     *
     * @param collectionId
     * @return
     */
    Integer presentCount(@Param("collectionId") Long collectionId, @Param("userId") Long userId, @Param("maxId") Long maxId);

    /**
     * 寄售列表(品牌孵化/自由二级)
     *
     * @param page
     * @param collectionId
     * @param sortType
     * @return
     */
    Page<MarketConsignPageV3_7VO> marketConsignPagev3_7(Page page, @Param("collectionId") Long collectionId, @Param("sortType") Integer sortType, @Param("shareUserId") Long shareUserId);

    Page<MarketConsignPageV3_7VO> marketConsignPageForConsensus(Page page, @Param("collectionId") Long collectionId, @Param("sortType") Integer sortType, @Param("shareUserId") Long shareUserId);

    Page<MarketConsignPageV3_7VO> marketConsignPageNotInConsensus(Page page, @Param("collectionId") Long collectionId, @Param("sortType") Integer sortType, @Param("shareUserId") Long shareUserId);

    Page<MarketConsignPageV3_7VO> marketConsignPagev3_7ForMarket(Page page, @Param("collectionId") Long collectionId, @Param("sortType") Integer sortType, @Param("shareUserId") Long shareUserId);

    /**
     * 根据市场商品id获取合约
     *
     * @param id
     * @return
     */
    ConsignmentSalesIdGetParticularsVO consignmentSalesIdGetParticulars(@Param("id") Long id);

    @Select("select max(id) from sc_second_collection")
    Long getMaxId();

//    @Select("select collection_id,min(sales_price) as sales_price from sc_second_collection where is_delete=0 and market_type=2 and status=1 group by collection_id ")
//    List<SecondCollection> getLowestPrice();

    @Select("select  min(sales_price) as sales_price from sc_second_collection where is_delete=0 and market_type=2 and status=1 and collection_id=#{collectionId} ")
    BigDecimal getCollectionLowestPrice(@Param("collectionId") Long collectionId);

    @Select("select  min(sales_price) as sales_price from sc_second_collection where is_delete=0 and market_type=2 and status=1 and collection_id=#{collectionId} and user_id not in (select user_id from sc_exit_consensus_user where status=1 and is_delete=0) ")
    BigDecimal getCollectionLowestPriceForConsensus(@Param("collectionId") Long collectionId);

    @Select("select  min(sales_price) as sales_price from sc_second_collection where is_delete=0 and market_type=2 and status=1 and collection_id=#{collectionId} and user_id  in (select user_id from sc_exit_consensus_user where status=1 and is_delete=0) ")
    BigDecimal getCollectionLowestPriceNotInConsensus(@Param("collectionId") Long collectionId);

    /**
     * 获取最新的最低价
     *
     * @param collectionId
     * @return
     */
    BigDecimal getMinPriceByCollectionId(@Param("collectionId") Long collectionId);

    BigDecimal getConsensusMinPriceByCollectionId(@Param("collectionId") Long collectionId);

    Integer getConsensusCountByCollectionIdAndPrice(@Param("collectionId") Long collectionId, @Param("price") BigDecimal price);

    /**
     * 获取寄售数量
     *
     * @param collectionId
     * @return
     */
    String getSalesCountByCollectionId(@Param("collectionId") Long collectionId);


    /**
     * 通过ID区间导出数据（非业务接口），注：包含逻辑删除数据
     *
     * @param begin 起始ID
     * @param end   结束ID
     * @return 数据列表
     */
    @Select("SELECT * FROM sc_second_collection WHERE id BETWEEN #{begin} AND #{end} ORDER BY id ASC")
    List<SecondCollection> export(@Param("begin") Long begin, @Param("end") Long end);

    @Select("SELECT count( * ) FROM sc_second_collection WHERE is_delete = 0 AND collection_id = #{collectionId} AND STATUS = 1 and user_id not in (select user_id from sc_exit_consensus_user where status=1 and is_delete=0) ")
    String getSalesCountByCollectionIdForConsensus(@Param("collectionId") Long collectionId);

    @Select("SELECT count( * ) FROM sc_second_collection WHERE is_delete = 0 AND collection_id = #{collectionId} AND STATUS = 1 and user_id in (select user_id from sc_exit_consensus_user where status=1 and is_delete=0) ")
    String getSalesCountByCollectionIdNotInConsensus(@Param("collectionId") Long collectionId);
}
